package cn.edu.ctbu.labtest.service;

import cn.edu.ctbu.labtest.dao.StudentRepository;
import cn.edu.ctbu.labtest.domain.Student;
import jdk.nashorn.internal.runtime.options.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Optional;

@Service
public class StudentService {

    @Autowired
    StudentRepository studentRepository;

    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Student> findAll(){

        return studentRepository.findAll();

    }

    /**
     * 读取所有的学生数据
     * @return
     */
    public Page<Student> findAll(Pageable pageable){

        return studentRepository.findAll(pageable);

    }


    /**
     * 读取所有的学生数据
     * @return
     */
    public Page<Student> findAll(Student  student,Pageable pageable){


        Example<Student> example = Example.of(student); //动态查询


        return studentRepository.findAll(example,pageable);

    }




    /**
     * 插入学生
     * @param student
     * @return
     */
    public Student insert(Student student){

        return studentRepository.save(student);
    }


    /**
     * 更新学生
     * @param student
     * @return
     */
    public Student update(Student student){


        return studentRepository.save(student);
    }

    public void delete(Student student){


        studentRepository.delete(student);
    }

    public void delete(Long id){

        Student student=new Student();
        student.setId(id);

        studentRepository.delete(student);

    }

    public Student getById(Long id){

        Student student=studentRepository.findById(id).orElse(null);;

        return student;

    }



}
